﻿@model IEnumerable<WaiGaoQiaoWeb.Models.GroupVM>
@{
    ViewBag.Title = "GroupIndex";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section Head
{
    <script type="text/javascript" language="javascript">
        var maiheight = document.documentElement.clientHeight;
        var mainWidth = document.documentElement.clientWidth - 10; // 减去边框和左边的宽度
        $(document).ready(function () {
            var otherpm = $("#title").height() + $("#findDiv").height(); // 290;
            var gh = maiheight - otherpm - 80;
            var option = {
                height: gh,
                width: mainWidth,
                url: '@Url.Action("GetGroupList")', // 参数要放在）括号的外面
                dataType: 'json',
                colModel: [
                    { display: 'ID', name: 'ID', width: 70, sortable: false, hide: true, align: 'left', iskey: true },
                    { display: '班组名称', name: 'Group_Name', width: 100, sortable: false, hide: false, align: 'left' },
                    { display: '备注', name: 'Remark', width: 200, sortable: false, hide: false, align: 'left' }
				],
                buttons: [
							{ name: 'Add', displayname: "新增班组", title: "新增班组", bclass: 'add', onpress: toolbarItemAdd_onclick },
                            { name: 'Delete', displayname: "删除班组", title: "删除班组", bclass: 'edit', onpress: toolbarItemDel_onclick },
                            { name: 'Edit', displayname: "修改班组", title: "修改班组", bclass: 'edit', onpress: toolbarItemEdit_onclick },
							{ separator: true }
						 ],
                title: false,
                singleselected: false,
                usepager: true,
                resizable: true,
                page: 1, //默认当前页  
                total: 1, //总页面数  
                useRp: true, //是否可以动态设置每页显示的结果数  
                rp: 20, // 每页默认的结果数  
                rpOptions: [10, 15, 20], //可选择设定的每页结果数
                showcheckbox: true,
                selectedonclick: true,
                rowbinddata: true,
                query: '',
                hideOnSubmit: true, //隐藏提交  
                autoload: true
            };
            $("#GroupList").flexigrid(option);
            $("#butfind").click(function (e) {
                var vgroupname = $("#txtGroupName").val();
                option.query = vgroupname;
                option.newp = 0;
                $('#GroupList').flexOptions(option).flexReload();
            });
            $("#txtGroupName").bind('keydown', function (e) {
                var key = e.which;
                if (key == 13) {
                    e.preventDefault();
                    var vgroupname = $("#txtGroupName").val();
                    option.query = vgroupname;
                    option.newp = 0;
                    $('#GroupList').flexOptions(option).flexReload();
                }
            });
            function toolbarItemAdd_onclick(cmd, grid) {
                OpenModelWindow('@Url.Action("AddGroup")',
                                   { width: 450, height: 300, caption: "新增班组", onclose: refresh });
            }  // end of toolbarItemAdd_onclick

            function toolbarItemEdit_onclick(cmd, grid) {
                var items = $("#GroupList").getSelectedRows();
                if (items.length == 0) {
                    showLoadingMsg("请选择要编辑的班组!", { left: 400, top: 6 }, true, 3000);
                    return false;
                }
                if (items.length > 1) {
                    showLoadingMsg("只能选择一个要编辑的班组!", { left: 400, top: 6 }, true, 3000);
                    return false;
                }
                var ID = items[0][0];
                OpenModelWindow('@Url.Action("EditGroup")?id=' + ID,
                 { width: 450, height: 300, caption: "修改班组", onclose: refresh });
            }
            function toolbarItemDel_onclick(cmd, grid) {
                var items = $("#GroupList").getSelectedRows();
                if (items.length == 0) {
                    showLoadingMsg("请选择要删除的班组!", { left: 400, top: 6 }, true, 3000);
                    return false;
                }
                if (!confirm("确定要删除班组吗？")) {
                    return false;
                }
                //                var rowcount = 0;
                //                for (var i = 0; i < items.length; i++) {
                //                    var ID = items[i][0];
                //                    $.ajax({
                //                        type: "POST",
                //                        url: '@Url.Action("DeleteGroup")',
                //                        data: { ID: ID },
                //                        dataType: "json",
                //                        async: false,
                //                        success: function (data) {
                //                            if (data) {
                //                                rowcount = rowcount + data;
                //                            }
                //                        },
                //                        error: function () { }
                //                    });   // end of ajax                    
                //                }
                //                refreshGrid();
                //                showLoadingMsg("共有" + rowcount.toString() + "个班组被删除!", { left: 400, top: 6 }, true, 3000);

                var ids = "";
                for (i = 0; i < items.length; i++) {
                    ids = ids + ',' + items[i][0].toString();
                }
                $.ajax({
                    url: '@Url.Action("DeleteGroup")',
                    type: "POST",
                    data: { id: ids },
                    success: function (callback) {
                        if (callback == 0) {
                            alert('班组已被使用不能删除');
                            return;
                        }
                        else {
                            showLoadingMsg("删除成功", { left: 400, top: 2 }, true, 3000);
                            refreshGrid();
                        }
                    },
                    error: function () {
                        showErrorTip("操作失败！", { left: 400, top: 2 }, true, 3000);
                    }
                });
            }
            function refreshGrid() {
                $("#GroupList").flexReload();
            }
            function refresh() {
                showLoadingMsg("保存成功", { left: 200, top: 6 }, true, 3000);
                $("#GroupList").flexReload();
            }
        });    //end document ready() 
    </script>
}
<div id="container_sx">
    <div id="mainContent">
        <div id="findDiv">
            <table>
                <tr>
                    <td style="text-align: right;">
                        &nbsp;班组名称：
                    </td>
                    <td style="text-align: left;">
                        @Html.TextBox("txtGroupName", "", new { @style = "width:180px;" })
                    </td>
                    <td colspan="2">
                        <a id="butfind" class="button" href="javascript:void(0);"><span>查询</span></a>
                    </td>
                </tr>
            </table>
        </div>
        <table id="GroupList" style="display: none;">
        </table>
    </div>
</div>
